/**
 * 
 */
package testy;

/**
 * @author pdrabik
 *
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/* ResizableComponent.java */

public class ResizableComponent extends JFrame {
	
	private static final Logger log = LoggerFactory
			.getLogger(ResizableComponent.class);

  /**
	 * 
	 */
	private static final long serialVersionUID = 3008632997356156807L;
private JPanel panel = new JPanel(null);
  private Resizable resizer;
  private JPanel innerPane = null;
  
  private Point loc = new Point(150,150);


  public ResizableComponent(JPanel pane) {
	  JScrollPane scroll = new JScrollPane();
	  scroll.getVerticalScrollBar().setUnitIncrement(65);
	  scroll.getVerticalScrollBar().setBlockIncrement(60);
	  scroll.getVerticalScrollBar().setValue(10);
	  scroll.setViewportView(panel);
	  panel.setPreferredSize(new java.awt.Dimension(2860, 2210));
      add(scroll);

      JPanel area = new JPanel(); 
      area.setBackground(Color.white);
//      if (innerPane==null)
//      resizer = new Resizable(area);
//      else 
    	  resizer = new Resizable(pane);
      resizer.setBounds(50, 50, pane.getWidth(), pane.getHeight());
      panel.add(resizer);

 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(new Dimension(350, 300));
      setTitle("CII Workbench");
//      setLocationRelativeTo(null);

      addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
          requestFocus();
          resizer.repaint();
        }
      });
  }
  
  public ResizableComponent(JPanel pane, Point location) {
	  JScrollPane scroll = new JScrollPane();
	  scroll.setViewportView(panel);
	  scroll.getVerticalScrollBar().setUnitIncrement(15);
	  panel.setBackground(Color.white);
	  panel.setPreferredSize(new java.awt.Dimension(2860, 2210));
      add(scroll);

      JPanel area = new JPanel(); 
      area.setBackground(Color.white);
//      if (innerPane==null)
//      resizer = new Resizable(area);
//      else 
    	  resizer = new Resizable(pane);
      resizer.setBounds(location.x, location.y, pane.getWidth(), pane.getHeight());
      panel.add(resizer);

 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(new Dimension(1000, 500));
      setTitle("CII Workbench");
//      setLocationRelativeTo(null);

      addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
          requestFocus();
          resizer.repaint();
        }
      });
  }
  
  public void addResizable(JPanel pane) {
	  JPanel area = new JPanel(); 
      area.setBackground(Color.white);
//      if (innerPane==null)
//      resizer = new Resizable(area);
//      else 
    	  resizer = new Resizable(pane);
      resizer.setBounds(loc.x, loc.x, pane.getWidth(), pane.getHeight());
      loc.x=loc.x+40;loc.y=loc.y+30;
      panel.add(resizer);
  }
  
  public void addResizable(JPanel pane, Point location) {
	  JPanel area = new JPanel(); 
      area.setBackground(Color.white);
//      if (innerPane==null)
//      resizer = new Resizable(area);
//      else 
    	  resizer = new Resizable(pane);
      resizer.setBounds(location.x, location.y, pane.getWidth(), pane.getHeight());
      setComponentPopupMenu(resizer,createPopupMenu());
      panel.add(resizer);
  }

  public static void main(String[] args) {
      ResizableComponent rc = new ResizableComponent(null);
      rc.setVisible(true);
  }

/**
 * @return the innerPane
 */
public JPanel getInnerPane() {
	return innerPane;
}

/**
 * @param innerPane the innerPane to set
 */
public void setInnerPane(JPanel innerPane) {
	this.innerPane = innerPane;
}

private void setComponentPopupMenu(
		final java.awt.Component parent,
		final javax.swing.JPopupMenu menu) {
		parent.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent e) {
				if (e.isPopupTrigger())
					menu.show(parent, e.getX(), e.getY());
			}
			public void mouseReleased(java.awt.event.MouseEvent e) {
				if (e.isPopupTrigger())
					menu.show(parent, e.getX(), e.getY());
			}
		});
	}

protected JPopupMenu createPopupMenu() {
	JPopupMenu jPopupMenu1=new JPopupMenu();
	JMenuItem menuRead = new JMenuItem();
		jPopupMenu1.add(menuRead);
		menuRead.setText("Start reading");
		menuRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
//				thCount = new Thread()
				log.debug("ckja menu");
				//TODO add your code for menuRead.actionPerformed
			}
		});
	return jPopupMenu1;
}

}
